package com.hlm.testredis.controller;

import com.hlm.testredis.mapper.TestDistributeMapper;
import com.hlm.testredis.util.DistributeUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program:test-redis
 * @description:每个分组的数量
 * @author: xinY
 * @create:2020-07-28 15:49
 **/
@RestController
public class TestCountPerGroupController {
    @Autowired
    private TestDistributeMapper mapper;

    @Value("${excelPath2}")
    private  String path;
    @GetMapping("/count")
    public void getData() {
        // 查询
        List<Double> totalList = mapper.findList4allRoomTemp();
        // 处理数据
        System.out.println("totallist" + totalList.size());
        double[] metas = new double[totalList.size()];
        for (int i = 0; i < totalList.size(); i++) {
            metas[i] = totalList.get(i);
        }
        // 分组后数据属性
        Map<String, Double> infoMap = DistributeUtil.infoGroup(metas, 3, 15);

        // 分组分布
        Map<Double, Integer> resultMap = DistributeUtil.countPerGroup(metas,3,15);
        System.out.println(resultMap.size());

        //
//        Map<Double,Double> finalMap = DistributeUtil.distribute(lessMetas);
        // 写入excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row0 = sheet.createRow(0);
        row0.createCell(1).setCellValue("x");
        row0.createCell(2).setCellValue("y");
        row0.createCell(3).setCellValue("avg");
        row0.createCell(4).setCellValue(infoMap.get("average"));
        row0.createCell(5).setCellValue("variance");
        row0.createCell(6).setCellValue(infoMap.get("variance"));
        row0.createCell(7).setCellValue("upLimitGroup:");
        row0.createCell(8).setCellValue(infoMap.get("upLimitGroup"));
        row0.createCell(9).setCellValue("downLimitGroup:");
        row0.createCell(10).setCellValue(infoMap.get("downLimitGroup"));
        int j = 0;
        for (Double key : resultMap.keySet()) {
            HSSFRow row = sheet.createRow(j + 1);
            row.createCell(1).setCellValue(key);
            row.createCell(2).setCellValue(resultMap.get(key));
            j++;
        }
        // 关闭 excel
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(path);
            workbook.write(fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
